<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class WxCardsController extends Controller
{
    const AUTH_FAIL_CODE = 403;
    const PARAMS_FAIL_CODE = 400;
    const SUCCESS_CODE = 200;

    public function addWxcard(Request $request)
    {
        $user = session(config('services.wechat.wauth_session_name'));
        if(empty($user)){
            return response()->json([
                'err_code' => self::AUTH_FAIL_CODE,
                'err_msg' => '由于长时未操作，已自动为您退出，请刷新后重试'
            ]);
        }

        if(empty($request->card_id)) return response()->json([
            'err_code' => self::PARAMS_FAIL_CODE,
            'err_msg' => '未找到属于您的卡券，请刷新后重试'
        ]);

        $cards = [
            ['card_id' =>$request->card_id, 'code' => uniqid($user['id'].'_')]
        ];

        return response()->json([
            'err_code'=>self::SUCCESS_CODE,
            'err_msg'=>json_decode(app('EasyWechatOfficial')->card->jssdk->assign($cards),true)
        ]);
    }

}
